package com.google.android.gms.ads.internal;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import com.google.android.gms.ads.internal.gmsg.zzt;
import com.google.android.gms.common.internal.Hide;
import com.google.android.gms.dynamic.IObjectWrapper;
import com.google.android.gms.dynamic.zzn;
import com.google.android.gms.internal.zzabh;
import com.google.android.gms.internal.zzahd;
import com.google.android.gms.internal.zzahw;
import com.google.android.gms.internal.zzaof;
import com.google.android.gms.internal.zzpk;
import com.google.android.gms.internal.zzpm;
import com.google.android.gms.internal.zzqs;
import com.google.android.gms.internal.zzqt;
import com.google.android.gms.internal.zzwr;
import com.google.android.gms.internal.zzwu;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@Hide
@zzabh
public final class zzar {
    static zzt<zzaof> zza(@Nullable zzwr zzwr, @Nullable zzwu zzwu, zzab zzab) {
        return new zzaw(zzwr, zzab, zzwu);
    }

    private static String zza(@Nullable Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        if (bitmap == null) {
            zzahw.zzcz("Bitmap is null. Returning empty string");
            return "";
        }
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        String encodeToString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0);
        String valueOf = String.valueOf("data:image/png;base64,");
        String valueOf2 = String.valueOf(encodeToString);
        return valueOf2.length() != 0 ? valueOf.concat(valueOf2) : new String(valueOf);
    }

    private static String zza(@Nullable zzqs zzqs) {
        if (zzqs == null) {
            zzahw.zzcz("Image is null. Returning empty string");
            return "";
        }
        try {
            Uri uri = zzqs.getUri();
            if (uri != null) {
                return uri.toString();
            }
        } catch (RemoteException unused) {
            zzahw.zzcz("Unable to get image uri. Trying data uri next");
        }
        return zzb(zzqs);
    }

    private static JSONObject zza(@Nullable Bundle bundle, String str) throws JSONException {
        String valueOf;
        String str2;
        JSONObject jSONObject = new JSONObject();
        if (bundle == null || TextUtils.isEmpty(str)) {
            return jSONObject;
        }
        JSONObject jSONObject2 = new JSONObject(str);
        Iterator<String> keys = jSONObject2.keys();
        while (keys.hasNext()) {
            String next = keys.next();
            if (bundle.containsKey(next)) {
                if ("image".equals(jSONObject2.getString(next))) {
                    Object obj = bundle.get(next);
                    if (obj instanceof Bitmap) {
                        valueOf = zza((Bitmap) obj);
                    } else {
                        str2 = "Invalid type. An image type extra should return a bitmap";
                        zzahw.zzcz(str2);
                    }
                } else if (bundle.get(next) instanceof Bitmap) {
                    str2 = "Invalid asset type. Bitmap should be returned only for image type";
                    zzahw.zzcz(str2);
                } else {
                    valueOf = String.valueOf(bundle.get(next));
                }
                jSONObject.put(next, valueOf);
            }
        }
        return jSONObject;
    }

    static final /* synthetic */ void zza(zzpk zzpk, String str, zzaof zzaof, zzaof zzaof2, boolean z) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("headline", zzpk.getHeadline());
            jSONObject.put(TtmlNode.TAG_BODY, zzpk.getBody());
            jSONObject.put("call_to_action", zzpk.getCallToAction());
            jSONObject.put("price", zzpk.getPrice());
            jSONObject.put("star_rating", String.valueOf(zzpk.getStarRating()));
            jSONObject.put("store", zzpk.getStore());
            jSONObject.put("icon", zza(zzpk.zzkc()));
            JSONArray jSONArray = new JSONArray();
            List<Object> images = zzpk.getImages();
            if (images != null) {
                for (Object zzd : images) {
                    jSONArray.put(zza(zzd(zzd)));
                }
            }
            jSONObject.put("images", jSONArray);
            jSONObject.put("extras", zza(zzpk.getExtras(), str));
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("assets", jSONObject);
            jSONObject2.put("template_id", "2");
            zzaof.zzb("google.afma.nativeExpressAds.loadAssets", jSONObject2);
        } catch (JSONException e) {
            zzahw.zzc("Exception occurred when loading assets", e);
        }
    }

    static final /* synthetic */ void zza(zzpm zzpm, String str, zzaof zzaof, zzaof zzaof2, boolean z) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("headline", zzpm.getHeadline());
            jSONObject.put(TtmlNode.TAG_BODY, zzpm.getBody());
            jSONObject.put("call_to_action", zzpm.getCallToAction());
            jSONObject.put("advertiser", zzpm.getAdvertiser());
            jSONObject.put("logo", zza(zzpm.zzkj()));
            JSONArray jSONArray = new JSONArray();
            List<Object> images = zzpm.getImages();
            if (images != null) {
                for (Object zzd : images) {
                    jSONArray.put(zza(zzd(zzd)));
                }
            }
            jSONObject.put("images", jSONArray);
            jSONObject.put("extras", zza(zzpm.getExtras(), str));
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("assets", jSONObject);
            jSONObject2.put("template_id", "1");
            zzaof.zzb("google.afma.nativeExpressAds.loadAssets", jSONObject2);
        } catch (JSONException e) {
            zzahw.zzc("Exception occurred when loading assets", e);
        }
    }

    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v11, resolved type: java.lang.Object} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r9v2, resolved type: android.view.View} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r5v10, resolved type: java.lang.Object} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r9v5, resolved type: android.view.View} */
    /* JADX WARNING: Multi-variable type inference failed */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x013c  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static boolean zza(com.google.android.gms.internal.zzaof r26, com.google.android.gms.internal.zzvw r27, java.util.concurrent.CountDownLatch r28) {
        /*
            r1 = r26
            r2 = r27
            r7 = r28
            r8 = 0
            android.view.View r3 = r26.getView()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r3 != 0) goto L_0x0014
            java.lang.String r1 = "AdWebView is null"
        L_0x000f:
            com.google.android.gms.internal.zzahw.zzcz(r1)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            goto L_0x013a
        L_0x0014:
            r4 = 4
            r3.setVisibility(r4)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzvp r3 = r2.zzcje     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.util.List<java.lang.String> r3 = r3.zzchr     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r3 == 0) goto L_0x0129
            boolean r4 = r3.isEmpty()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r4 == 0) goto L_0x0026
            goto L_0x0129
        L_0x0026:
            com.google.android.gms.internal.zzapu r4 = r26.zzua()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r5 = "/nativeExpressAssetsLoaded"
            com.google.android.gms.ads.internal.zzau r6 = new com.google.android.gms.ads.internal.zzau     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r6.<init>(r7)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r4.zza((java.lang.String) r5, (com.google.android.gms.ads.internal.gmsg.zzt<? super com.google.android.gms.internal.zzaof>) r6)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzapu r4 = r26.zzua()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r5 = "/nativeExpressAssetsLoadingFailed"
            com.google.android.gms.ads.internal.zzav r6 = new com.google.android.gms.ads.internal.zzav     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r6.<init>(r7)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r4.zza((java.lang.String) r5, (com.google.android.gms.ads.internal.gmsg.zzt<? super com.google.android.gms.internal.zzaof>) r6)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzwi r4 = r2.zzcjf     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzwr r4 = r4.zzmp()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzwi r5 = r2.zzcjf     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzwu r5 = r5.zzmq()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r6 = "2"
            boolean r6 = r3.contains(r6)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r9 = 0
            if (r6 == 0) goto L_0x00b1
            if (r4 == 0) goto L_0x00b1
            com.google.android.gms.internal.zzpk r3 = new com.google.android.gms.internal.zzpk     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r11 = r4.getHeadline()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.util.List r12 = r4.getImages()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r13 = r4.getBody()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzqs r14 = r4.zzkc()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r15 = r4.getCallToAction()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            double r16 = r4.getStarRating()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r18 = r4.getStore()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r19 = r4.getPrice()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r20 = 0
            android.os.Bundle r21 = r4.getExtras()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r22 = 0
            com.google.android.gms.dynamic.IObjectWrapper r5 = r4.zzmx()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r5 == 0) goto L_0x0094
            com.google.android.gms.dynamic.IObjectWrapper r5 = r4.zzmx()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.Object r5 = com.google.android.gms.dynamic.zzn.zzy(r5)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r9 = r5
            android.view.View r9 = (android.view.View) r9     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
        L_0x0094:
            r23 = r9
            com.google.android.gms.dynamic.IObjectWrapper r24 = r4.zzkh()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r25 = 0
            r10 = r3
            r10.<init>(r11, r12, r13, r14, r15, r16, r18, r19, r20, r21, r22, r23, r24, r25)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzvp r4 = r2.zzcje     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r4 = r4.zzchq     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzapu r5 = r26.zzua()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.ads.internal.zzas r6 = new com.google.android.gms.ads.internal.zzas     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r6.<init>(r3, r4, r1)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
        L_0x00ad:
            r5.zza((com.google.android.gms.internal.zzapv) r6)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            goto L_0x0108
        L_0x00b1:
            java.lang.String r4 = "1"
            boolean r3 = r3.contains(r4)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r3 == 0) goto L_0x0125
            if (r5 == 0) goto L_0x0125
            com.google.android.gms.internal.zzpm r3 = new com.google.android.gms.internal.zzpm     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r11 = r5.getHeadline()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.util.List r12 = r5.getImages()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r13 = r5.getBody()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzqs r14 = r5.zzkj()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r15 = r5.getCallToAction()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r16 = r5.getAdvertiser()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r17 = 0
            android.os.Bundle r18 = r5.getExtras()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r19 = 0
            com.google.android.gms.dynamic.IObjectWrapper r4 = r5.zzmx()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r4 == 0) goto L_0x00ee
            com.google.android.gms.dynamic.IObjectWrapper r4 = r5.zzmx()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.Object r4 = com.google.android.gms.dynamic.zzn.zzy(r4)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r9 = r4
            android.view.View r9 = (android.view.View) r9     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
        L_0x00ee:
            r20 = r9
            com.google.android.gms.dynamic.IObjectWrapper r21 = r5.zzkh()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r22 = 0
            r10 = r3
            r10.<init>(r11, r12, r13, r14, r15, r16, r17, r18, r19, r20, r21, r22)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzvp r4 = r2.zzcje     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r4 = r4.zzchq     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzapu r5 = r26.zzua()     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.ads.internal.zzat r6 = new com.google.android.gms.ads.internal.zzat     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            r6.<init>(r3, r4, r1)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            goto L_0x00ad
        L_0x0108:
            com.google.android.gms.internal.zzvp r3 = r2.zzcje     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r3 = r3.zzcho     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            com.google.android.gms.internal.zzvp r2 = r2.zzcje     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            java.lang.String r2 = r2.zzchp     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            if (r2 == 0) goto L_0x011b
            java.lang.String r4 = "text/html"
            java.lang.String r5 = "UTF-8"
            r6 = 0
            r1.loadDataWithBaseURL(r2, r3, r4, r5, r6)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
            goto L_0x0122
        L_0x011b:
            java.lang.String r2 = "text/html"
            java.lang.String r4 = "UTF-8"
            r1.loadData(r3, r2, r4)     // Catch:{ RemoteException -> 0x0133, RuntimeException -> 0x012d }
        L_0x0122:
            r1 = 1
            r8 = r1
            goto L_0x013a
        L_0x0125:
            java.lang.String r1 = "No matching template id and mapper"
            goto L_0x000f
        L_0x0129:
            java.lang.String r1 = "No template ids present in mediation response"
            goto L_0x000f
        L_0x012d:
            r0 = move-exception
            r1 = r0
            r28.countDown()
            throw r1
        L_0x0133:
            r0 = move-exception
            r1 = r0
            java.lang.String r2 = "Unable to invoke load assets"
            com.google.android.gms.internal.zzahw.zzc(r2, r1)
        L_0x013a:
            if (r8 != 0) goto L_0x013f
            r28.countDown()
        L_0x013f:
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.gms.ads.internal.zzar.zza(com.google.android.gms.internal.zzaof, com.google.android.gms.internal.zzvw, java.util.concurrent.CountDownLatch):boolean");
    }

    private static String zzb(zzqs zzqs) {
        try {
            IObjectWrapper zzkb = zzqs.zzkb();
            if (zzkb == null) {
                zzahw.zzcz("Drawable is null. Returning empty string");
                return "";
            }
            Drawable drawable = (Drawable) zzn.zzy(zzkb);
            if (drawable instanceof BitmapDrawable) {
                return zza(((BitmapDrawable) drawable).getBitmap());
            }
            zzahw.zzcz("Drawable is not an instance of BitmapDrawable. Returning empty string");
            return "";
        } catch (RemoteException unused) {
            zzahw.zzcz("Unable to get drawable. Returning empty string");
            return "";
        }
    }

    /* access modifiers changed from: private */
    public static void zzc(zzaof zzaof) {
        View.OnClickListener onClickListener = zzaof.getOnClickListener();
        if (onClickListener != null) {
            onClickListener.onClick(zzaof.getView());
        }
    }

    @Nullable
    private static zzqs zzd(Object obj) {
        if (obj instanceof IBinder) {
            return zzqt.zzk((IBinder) obj);
        }
        return null;
    }

    @Nullable
    public static View zze(@Nullable zzahd zzahd) {
        if (zzahd == null) {
            zzahw.e("AdState is null");
            return null;
        } else if (zzf(zzahd) && zzahd.zzcnm != null) {
            return zzahd.zzcnm.getView();
        } else {
            try {
                IObjectWrapper view = zzahd.zzcjf != null ? zzahd.zzcjf.getView() : null;
                if (view != null) {
                    return (View) zzn.zzy(view);
                }
                zzahw.zzcz("View in mediation adapter is null.");
                return null;
            } catch (RemoteException e) {
                zzahw.zzc("Could not get View from mediation adapter.", e);
                return null;
            }
        }
    }

    public static boolean zzf(@Nullable zzahd zzahd) {
        return (zzahd == null || !zzahd.zzcto || zzahd.zzcje == null || zzahd.zzcje.zzcho == null) ? false : true;
    }
}
